namespace Project.MVC.PhanQuyen.Services
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using IDAL;
    using Model;
    using MsSQLDAL;

	public class PQNhomSuDungCongViecServices : IPQNhomSuDungCongViecServices
	{
		IPQNhomSuDungCongViec repository = null;
        
		public PQNhomSuDungCongViecServices()
            : this( new PQNhomSuDungCongViecRepository() )
		{                        
		}
        
        public PQNhomSuDungCongViecServices( IPQNhomSuDungCongViec rep )
        {
            repository = rep;
            if(repository == null)
                throw new InvalidOperationException("Repository cann't be null");
        }
		
        #region IPQNhomSuDungCongViecServices Members
        public PQNhomSuDungCongViecInfo GetByKey( int idNhomSuDung, int idCongViec )
		{
            try
			{
                return repository.GetByKey(idNhomSuDung, idCongViec);
			}
			catch(Exception)
			{
				throw;
			}
		}
		#endregion
		
        #region IServicesBase<PQNhomSuDungCongViecInfo> Members
        public List<PQNhomSuDungCongViecInfo> GetList()
		{
            try
			{
                return repository.GetList().ToList();
			}
			catch(Exception)
			{
				throw;
			}
		}
        
        public List<PQNhomSuDungCongViecInfo> GetList(int start, int offset)
        {
            try
            {
                return repository.GetList().Skip(start).Take(offset).ToList();
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public bool Insert(PQNhomSuDungCongViecInfo value)
        {
            try
            {
                var res = repository.Insert(value);
                if (res)
                {
                    SaveChange();                   
                }
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public bool Update(PQNhomSuDungCongViecInfo value)
        {
            try
            {
                var res = repository.Update(value);
                if (res)
                {
                    SaveChange();
                }
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public bool Delete(PQNhomSuDungCongViecInfo value)
        {
            try
            {
                var res = repository.Delete(value);
                if (res)
                {
                    SaveChange();
                }
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public void SaveChange()
        {
            try
            {
                repository.Commit();                
            }
            catch (Exception)
            {
                throw;
            }
        }
        
        public int Count()
        {
            try
            {
                return repository.Count();
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion       
	}
}
